package task.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

import android.os.Environment;
import android.util.Log;

public class CategoryFacade {
	
	private CategoryList categories;
	private LoginFacade loginFacade;
	private String activeCategory;
	private int sortState = 0;
	private int sortMonth;
	private int sortDay;
	private int sortYear;
	
	private static CategoryFacade INSTANCE;
	
	public static CategoryFacade getInstance(){
		
		if(INSTANCE == null){
			INSTANCE = new CategoryFacade();
		}
		
		return INSTANCE;
	}
	
	public CategoryFacade(){
		
		loginFacade = LoginFacade.getInstance();
		
		try {
			loadCategories();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(categories == null){
			categories = new CategoryList();
		}
		
	}
	
	public void loadCategories() throws IOException {
		ObjectInputStream ois = null;
		try {
			//we are using external storage, so we need to get the location
			//be sure you have the correct privileges 
			File file = new File(Environment.getExternalStorageDirectory() + "/" + loginFacade.getActiveUser() + "/category1.bin");
			ois = new ObjectInputStream(new FileInputStream(file));
			categories = (CategoryList)ois.readObject();
		} catch (StreamCorruptedException e) {
			Log.e("APPLICATION FACADE", "Corrupted binary file");
		} catch (FileNotFoundException e) {
			Log.e("APPLICATION FACADE", "Binary File not found");
			System.out.println("Binary File not found");
		} catch (IOException e) {
			Log.e("APPLICATION FACADE", "General IO Error");
		} catch (ClassNotFoundException e) {
			Log.e("APPLICATION FACADE", "Class not found");
		} finally {
			if (ois != null)
				ois.close();
		}
		
	}
	
	public void saveCategories() throws IOException {
		ObjectOutputStream oos = null;
		try {
			File file = new File(Environment.getExternalStorageDirectory() + "/" + loginFacade.getActiveUser() + "/category1.bin");
			//System.out.println("File created");
			oos = new ObjectOutputStream(new FileOutputStream(file));
			//System.out.println("ObjectOutputStream instantiated");
			oos.writeObject(categories);
			//System.out.println("users object written to file");
			oos.flush();
			//System.out.println("objectOutputStream flushed");
		} catch (IOException e) {
			Log.e("TaskManagerActivity", "Binary file input general error");
			e.printStackTrace();
		} finally {
			if (oos != null) {
				oos.close();
			}
		}
	}
	
	public String[] getCategoryList(){
		return categories.getNames(sortState, sortMonth, sortDay, sortYear);
	}
	
	public CategoryList getCategories(){
		return categories;
	}
	
	public void addNewCategory(Category c){
		//System.out.println("" + categories);
		//if(!categories.contains(c)) {
			categories.add(c);
		//}
	}

	public TaskList getTasksFromCategory(String selectedCat) {
		return categories.getTasksFromCategory(selectedCat);
	}
	
	public void setActiveCategory(String category) {
		activeCategory = category;
	}
	
	public String getActiveCategory(){
		return activeCategory;
	}

	public void setState(int state) {
		this.sortState = state;
		
	}

	public void setSortDate(int mMonth, int mDay, int mYear) {
		this.sortMonth = mMonth;
		this.sortDay = mDay;
		this.sortYear = mYear;
		
	}

	public void addTaskToCategory(String category, Task newTask) {
		Category tempCat = categories.getCategory(category);
		if(tempCat == null){
			categories.add(new Category(category));
			System.out.println("Category List Item: " + categories.getCategory(category) + " " + categories.getCategory("bla") + " " + ((Category) (categories.getCategoryArray().get(0))).getName());
			tempCat = categories.getCategory(category);
		}
		System.out.println("IS TEMPCAT NULL?????: " + tempCat);
		categories.addTask(tempCat, newTask);
		
	}

	public int getSortState() {
		// TODO Auto-generated method stub
		return sortState;
	}

	public Category getCategoryOfTask(String task) {
		// TODO Auto-generated method stub
		return categories.getCategoryOfTask(task);
	}

	


}
